package com.ruoyi.business.controller;

import com.ruoyi.business.domain.bo.QuestionBo;
import com.ruoyi.business.domain.bo.UserBo;
import com.ruoyi.business.domain.vo.UserVo;
import com.ruoyi.business.service.IUserService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.InnerObjVo;
import com.ruoyi.system.mapper.SysTableObjMapper;
import com.ruoyi.system.service.ISysOssService;
import com.sun.org.apache.regexp.internal.RE;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.A;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * @author 最爱喝兽奶
 * @create 2022/7/13
 **/
@Api("用户操作的controller")
@RestController
@RequestMapping("/business/user")
public class UserController {

    private final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private IUserService iUserService;

    @Autowired
    private ISysOssService ossService;

    @GetMapping("/list")
    public AjaxResult getUserList(UserBo bo) {
        try {
            TableDataInfo tableDataInfo = iUserService.getUserList(bo);
            return AjaxResult.success(tableDataInfo);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("查询用户信息异常", e);
            }
            return AjaxResult.error();
        }
    }

    @ApiOperation("获取用户基本信息")
    @GetMapping()
    public AjaxResult getUserInfo(@RequestParam("userId") String userId) {
        UserVo vo = iUserService.getVoById(userId);
        return AjaxResult.success(vo);
    }

    /**
     * 获取分组的映射信息
     * @return
     */
    @ApiOperation("获取分组的映射信息")
    @GetMapping("/group/ref")
    public AjaxResult queryGroupRef() {
        List<InnerObjVo> innerObjVos = iUserService.queryGroupRef();
        return AjaxResult.success(innerObjVos);
    }

    @ApiOperation("插入用户信息")
    @PostMapping
    public AjaxResult insertUser(@RequestBody @Validated(AddGroup.class) UserBo bo) {
        iUserService.insertUser(bo);
        return AjaxResult.success();
    }

    @ApiOperation("更改用户信息")
    @PutMapping
    public AjaxResult updateUser(@RequestBody @Validated(EditGroup.class) UserBo bo) {
        iUserService.updateUser(bo);
        return AjaxResult.success();
    }

    @ApiOperation("删除用户信息")
    @DeleteMapping("/{id}")
    public AjaxResult deleteUser(@PathVariable("id") String id) {
        iUserService.deleteUser(id);
        return AjaxResult.success();
    }

    /**
     * 分页查询用户关联对象
     */
    @ApiOperation("查询课程关联对象")
    @GetMapping("/refTable")
    public AjaxResult refTable(@NotEmpty(message = "关联对象不能为空") String refColumn, Integer pageSize, Integer currentPage, String filterContent, String filterId) {
        return AjaxResult.success(iUserService.selectRefTableByName(refColumn, pageSize, currentPage, filterContent, filterId));
    }

    @ApiOperation("导入用户数据")
    @PostMapping("/import")
    public AjaxResult importUserData(@RequestBody Map<String, Object> map) throws Exception {
        if (null != map.get("file")) {
            ExcelUtil<UserBo> util = new ExcelUtil<UserBo>(UserBo.class);
            List<UserBo> boList = util.importExcel(ossService.getInputStreamByOssId((Serializable) map.get("file")));
            if ((Boolean) map.get("isCover")) {
                iUserService.insertUserListCover(boList);
            }else {
                iUserService.insertUserList(boList);
            }
            return AjaxResult.success();
        } else {
            return AjaxResult.error("请传入文件信息");
        }
    }

}
